package com.dxy.app.widget.loading_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import com.dxy.app.R;
import com.dxy.app.widget.ThreadManager;

/**
  *Created by duxingyu on 2017/7/24.18:47
  *Email: 
  *Description:文字类型的进度控件
  **/

public class TextLoadingView extends View implements View.OnClickListener{

  private static final String TAG = "TextLoadingView";
  /**
   * 画笔
   * */
  private TextPaint paint;
  /**
   * 进度条文本内容
   * */
  private String text;
  /**
   * 文本内容的字体尺寸
   * */
  private float textSize;
  /**
   * 当前的进度，初始值为0
   * */
  private float offsetX =0;
  /**
   * 颜色资源
   * */
  private int colorDef,//默认的颜色
      colorPro;//进度的颜色
  /**
   * 控件宽高
   * */
  private float width,//宽度,文本的宽度
      height;//高度，文本的高度
  public TextLoadingView(Context context) {
    super(context);
    init(null);
  }

  public TextLoadingView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray array =context.obtainStyledAttributes(attrs, R.styleable.TextLoadingView);
    init(array);
  }

  public TextLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray array =context.obtainStyledAttributes(attrs, R.styleable.TextLoadingView);
    init(array);
  }
  private void init(TypedArray array){
    float scale =getContext().getResources().getDisplayMetrics().scaledDensity;
    if(array!=null){
      text =array.getString(R.styleable.TextLoadingView_loading_text);
      textSize =array.getDimension(R.styleable.TextLoadingView_loading_text_size,10f);
      colorDef =array.getColor(R.styleable.TextLoadingView_loading_default_color, Color.BLACK);
      colorPro =array.getColor(R.styleable.TextLoadingView_loading_progress_color,Color.RED);
    }else{
      textSize =50f;
      text ="正在下载中...";
      colorDef =Color.BLACK;
      colorPro =Color.RED;
    }

    initParams();
    textSize =textSize*scale;
    paint.setTextSize(textSize);

    width =paint.measureText(text)+0.5f;
    height =paint.descent()-paint.ascent();
    setOnClickListener(this);
  }
  /**
   * 初始化一些参数
   * */
  private void initParams(){
    paint =new TextPaint();
    paint.setAntiAlias(true);
  }

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension((int)width, (int)height);
  }

  @Override protected void onDraw(Canvas canvas) {
    paint.setColor(colorDef);
    canvas.drawText(text,0,height-paint.descent(),paint);//android中绘制文本内容的时候，y轴的基准线自文本的descent分割线开始的，所以进行计算时要排除descent的偏移量,只算ascent偏移量

    RectF rShadow =new RectF(0,0,offsetX,height);
    canvas.clipRect(rShadow, Region.Op.INTERSECT);

    paint.setColor(colorPro);
    canvas.drawText(text,0,height-paint.descent(),paint);
  }
  /**
   * 设置当前的进度
   * */
  public void setProgress(float percent){
    offsetX =width*percent;
    invalidate();
  }

  private Handler handler =new Handler(){
    private float percent =0;
    @Override public void handleMessage(Message msg) {
      percent+=0.02f;
      setProgress(percent);
      if(percent>=1.0f){
        inGrowthing =false;
      }
    }
  };
  private boolean inGrowthing =false;
  @Override public void onClick(View v) {
    if(inGrowthing){
      return;
    }
    inGrowthing =true;
    ThreadManager.getShortPool().execute(()->{
      while (inGrowthing){
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        handler.sendEmptyMessage(0);
      }
    });
  }
}
